//
//  RWModifyPasswordViewController.m
//  RedWine
//
//  Created by Liufangfang on 2017/1/6.
//  Copyright © 2017年 HuiBangKe. All rights reserved.
//

#import "RWModifyPasswordViewController.h"
#import "FTYInfoVertifyTools.h"

@interface RWModifyPasswordViewController ()<UITextFieldDelegate>{

    NSArray *imageIcons;
    NSArray *textFields;
}

@property (nonatomic, strong) UITextField *oldPasswordTF;
@property (nonatomic, strong) UITextField *nPasswordTF;
@property (nonatomic, strong) UITextField *reNewPasswordTF;

@end

@implementation RWModifyPasswordViewController

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initObjects];
    [self setNavigation];
    [self setModifyPasswordUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Load Datas

- (void)initObjects{
    
    imageIcons = @[@"code_icon", @"code_icon", @"code_icon"];
    textFields = @[@"请输入原密码", @"请输入新密码", @"请确认新密码"];
}

- (void)loadDatas{
    
    
}

#pragma mark - Config UI

- (void)setNavigation{
 
    self.title = @"修改密码";
}

- (void)setModifyPasswordUI{
    
    _modifyPasswordTV = [[FTYTouchTableView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, FTY_SCREEN_HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:_modifyPasswordTV];
    _modifyPasswordTV.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
    _modifyPasswordTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    _modifyPasswordTV.scrollEnabled = NO;
    _modifyPasswordTV.dataSource = self;
    _modifyPasswordTV.delegate = self;
    _modifyPasswordTV.touchDelegate = self;
    _modifyPasswordTV.tableFooterView = [self setFooterUI];
}

#pragma mark - Config Invoke Methods

- (UIView *)setFooterUI{

    UIView *footerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, FTY_SCREEN_HEIGHT)];
    
    FTYButton *modiftyBtn = [FTYButton createButtonWithTitle:@"确认修改" tColor:[UIColor blackColor] fontSize:15.*FTY_WIDTH_BASE backGroundColor:[FTYColor getMainColorWithAlpha:1.] cornerRadius:15.*FTY_WIDTH_BASE];
    [footerV addSubview:modiftyBtn];
    [modiftyBtn addTarget:self action:@selector(modiftyBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [modiftyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(40.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(10.*FTY_WIDTH_BASE);
        make.width.mas_equalTo(FTY_SCREEN_WIDTH - 15.*FTY_WIDTH_BASE *2);
        make.height.mas_equalTo(40.*FTY_HEIGHT_BASE);
    }];
    
    return footerV;
}

#pragma mark - Notificaiton Method

#pragma mark - Notificaiton Invoke Methods

#pragma mark - <UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 40.*FTY_HEIGHT_BASE;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 60.*FTY_HEIGHT_BASE;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * const modifyPasswordID = @"modifyPasswordID";
    UITableViewCell *modifyPasswordCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:modifyPasswordID];
    
    NSInteger row = indexPath.row;
    modifyPasswordCell.imageView.image = [UIImage imageNamed:imageIcons[row]];
    
    UITextField *textField = [[UITextField alloc] init];
    [modifyPasswordCell addSubview:textField];
    textField.placeholder = textFields[row];
    textField.font = FTY_FONTTSIZE(15);
    textField.delegate = self;
    [textField sizeToFit];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(modifyPasswordCell);
        make.left.mas_equalTo(modifyPasswordCell.imageView.mas_right).offset(10.*FTY_WIDTH_BASE);
        make.right.mas_offset(-10.*FTY_WIDTH_BASE);
    }];
    
    if (0 == row) {
        _oldPasswordTF = textField;
    } else if(1 == row) {
        _nPasswordTF = textField;
    } else {
        _reNewPasswordTF = textField;
    }
    
    //底部线
    UIView *line = [[UIView alloc] init];
    [modifyPasswordCell addSubview:line];
    line.backgroundColor = [UIColor lightGrayColor];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_offset(0);
        make.height.mas_offset(.5*FTY_HEIGHT_BASE);
    }];
    
    return modifyPasswordCell;
}

#pragma mark - <UITableViewDelegate>

#pragma mark - <FTYTouchTableViewDelegate>

- (void)tableView:(UITableView *)tableView touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [_oldPasswordTF resignFirstResponder];
    [_nPasswordTF resignFirstResponder];
    [_reNewPasswordTF resignFirstResponder];
}

#pragma mark - Private Invoke Methods

- (void)modiftyBtnAction:(UIButton *)btn{

    NSString *oldPassword = _oldPasswordTF.text;
    NSString *nPassword = _nPasswordTF.text;
    NSString *reNewPassword = _reNewPasswordTF.text;
    
    if ([FTYInfoVertifyTools isnilVertify:oldPassword] || [FTYInfoVertifyTools isnilVertify:nPassword]) {
        [[FTYToastView alloc] addTitle:@"密码不能为空～" addView:self.view];
    } else if(![nPassword isEqualToString:reNewPassword]){
        [[FTYToastView alloc] addTitle:@"两次密码输入不一致～" addView:self.view];
    } else {
    
        [FTYHTTPManager postInfoWithSubUrl:[NSString stringWithFormat:@"/shop/ybkapi.php/?act=ybkapi&op=updatepassword&member_id=%@&password=%@&newpassword=%@", [FTYUser sharedUser].userID, oldPassword, reNewPassword] parameters:nil block:^(id result, NSError *error) {
            
            if (!error) {
                
                NSInteger code = [[[result objectForKey:@"err"] objectForKey:@"errorcode"] integerValue];
                NSString *info = [[result objectForKey:@"err"] objectForKey:@"errorinfo"];
                if (0 == code) {
                    [self modiftySuccess];
                } else {
                    [[FTYToastView alloc] addTitle:info addView:self.view];
                }
            } else {
                
                NSLog(@"====%@", error);
            }
        }];
    }
}

- (void)modiftySuccess{

    [[NSNotificationCenter defaultCenter] postNotificationName:@"ModifyPasswordSuccessNotification" object:nil];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

@end
